import json

from flask import request,jsonify

from utils.jwtc import token1
from flask import Blueprint
from utils.acl import acl
from utils.redosconn import r
from utils.comm import get_data
from utils.db import db1


bp_user = Blueprint('user',__name__)

# 登陆
@bp_user.route('/login',methods=['GET'])
def login():
    name = request.args.get("name")
    password = request.args.get("password")
    # print(name,password)
    sql = "select * from user where name='%s' and password='%s'"%(name,password)
    res = db1.find(sql)
    print(res)
    if res:
          vurl = acl.get_page_reslist(res['id'])
          interfacelist = acl.get_interface_resource(res['id'])
          vurl1 = vurl+interfacelist
          key = "interface"+str(res['id'])
          r.insert_values(key,interfacelist)
          token = token1.encode_jwt(int(res["id"]), 60 * 60, "123")
          return jsonify({'code':200,'userid':res['id'],"token":token,"vurl":vurl1})
    else:
        return jsonify({"code":20020,"msg":'登陆失败'})



# def getlist():
#     list = []
#     idlist = []
#     for i in data:
#         id=1['pid']
#         if id not in idlist:
#             list.append({"id":id,'name':i['pname'],'son':[]})
#             idlist.append(id)
#     #遍历顶级列表，在遍历数据，把子类加到顶级列表中
#     for j.value in enumerate(list):
#         for i in data:
#             if value['id'] == i['pid']:
#                 list[j]['son'].append({'id':i['id'],'name':i['name'],'url':i['url']})
#     return list















